#include "constants.h";
#include "R3F_DEBUG\Scripts\R3F_DEBUG_Configuration.h"
call compile preprocessFileLineNumbers "commonfunctions.sqf";

diag_log Format["World Desc: %1",getText (configFile>>"CfgWorlds">>worldName>> "description")];
diag_log Format["World Name: %1",getText (configFile>>"CfgWorlds">>worldName>> "worldname")];
diag_log Format["World Id: %1",getNumber (configFile>>"CfgWorlds">>worldName >> "worldid")];
diag_log Format["World Center: %1",getArray (configFile>>"CfgWorlds">>worldName >> "centerPosition")];

diag_log Format["base:[%1,%2,%3,%4],",(getMarkerPos "base1"), (getMarkerSize "base1") select 0, (getMarkerSize "base1") select 1, (markerDir "base1")];

diag_log Format["box1:[%1,%2],",(getMarkerPos "box1"), (markerDir "box1")];
diag_log Format["box2:[%1,%2],",(getMarkerPos "box2"), (markerDir "box2")];
diag_log Format["backpack:[%1,%2],",(getMarkerPos "backpack"), (markerDir "backpack")];

diag_log Format["aihut:[%1,%2],",(getMarkerPos "aihut"), (markerDir "aihut")];
diag_log Format["aispawn:[%1,%2],",(getMarkerPos "aispawn"), (markerDir "aispawn")];
diag_log Format["aivec:[%1,%2],",(getMarkerPos "aivec"), (markerDir "aivec")];
diag_log Format["aaradar:[%1,%2],",(getMarkerPos "aaradar"), (markerDir "aaradar")];

D_LOCATIONS = [];
D_HOUSES = [];
D_DONE_HOUSES = [];
D_CYCLING_BUSY = false;
D_CURRENT_BUILDING = objNull;

D_DONE_HOUSE_LIST = [];
D_DONE_POSITIONS_LIST = [];
D_BUILDING_INDEXER = [];
D_BUILDING_TABLE = [];

#include "infos\done_building_table.sqf";

// Spawn Sector list creation
_sector_check = true;
_sector_id = 0;
_missing = 0;
diag_log "";
diag_log "SPAWN SECTORS";
while { _sector_check } do {
	_sector_id = _sector_id + 1;
	_sector_name = Format["ss%1", _sector_id];
	_sector_pos = getMarkerPos _sector_name;
	if ((_sector_pos distance [0,0,0]) > 10) then {
		_spot_unique_name = MarkerText _sector_name;
		if (_spot_unique_name == "") then { _spot_unique_name = _sector_name; };
		_rectangle = ((markerShape _sector_name) == "RECTANGLE");
		// Print it
		diag_log Format["sector:[%1,[[%3,%4,%5,%6,%7]],'%2','%2'],",XF_SECTOR_TYPE_SPAWN,_spot_unique_name,_sector_pos, (getMarkerSize _sector_name) select 0, (getMarkerSize _sector_name) select 1, (markerDir _sector_name),_rectangle];
	} else {
		_missing = _missing + 1;
		if (_missing > 20) then {
			_sector_check = false;
		};
	};
};

// Missions Sector list creation
_sector_check = true;
_sector_id = 0;
_subsector_id = 0;
_missing = 0;
_sector_list = [];
diag_log "";
diag_log "MISSION SECTORS";
while { _sector_check } do {
	_sector_id = _sector_id + 1;
	
	_sector = [];
	_sector set [XF_SECTOR_ARRAY_TYPE, XF_SECTOR_TYPE_MISSIONS];
	_sector set [XF_SECTOR_ARRAY_SHAPE_ARRAY, []];
	
	_shape_array = [];
	_subsector_id = 0;
	_sub_sector_check = true;
	while { _sub_sector_check } do {
		_subsector_id = _subsector_id + 1;
		_sector_name = Format["ms%1_%2", _sector_id, _subsector_id];
		_sector_pos = getMarkerPos _sector_name;
		if ((_sector_pos distance [0,0,0]) > 10) then {
			_rectangle = ((markerShape _sector_name) == "RECTANGLE");
			// Add the shape to the list
			_shape_array set [count _shape_array, [_sector_pos, ((getMarkerSize _sector_name) select 0) * 2, ((getMarkerSize _sector_name) select 1) * 2, (markerDir _sector_name),_rectangle]];
		} else {
			_sub_sector_check = false;
		};	
	};
	if (count _shape_array > 0) then {
		_spot_unique_name = MarkerText Format["ms%1_1", _sector_id];;
		if (_spot_unique_name == "") then { _spot_unique_name = Format["ms%1", _sector_id]; };
		// Update the sector
		_sector set [XF_SECTOR_ARRAY_SHAPE_ARRAY, _shape_array];
		// Add it to the list
		_sector_list set [ count _sector_list, _sector];
		// Print it
		diag_log Format["sector:[%1,%3,'%2','%2'],",XF_SECTOR_TYPE_MISSIONS,_spot_unique_name,_shape_array];
	} else {
		_missing = _missing + 1;
		if (_missing > 20) then {
			_sector_check = false;
		};
	};	
};

diag_log "";
diag_log "[position,'display name','unique name',zoneSize,zoneType,sectorId,civilDensity,hasRoadBlock,[0,0]],";
diag_log "Zone Types:";
diag_log "  0= Town";
diag_log "  1= Village";
diag_log "  2= Lonely Building";
diag_log "  3= Special Site (radio tower, casltes, etc) This will disable civilian spawn";
diag_log "  4= Military Site (camp, airbase, etc) This will disable civilian spawn";
diag_log "  5= Industrial Site. Enables more trucks spawn";
diag_log "  6= Grassland (out of towns and mostly tree free)";
diag_log "  7= Forest (inside a forest without much buildings)";
diag_log "  8= Beach (on a coast without much buildings)";
diag_log "  9= Island Town (only reachable by sea)";
diag_log "  10= Mountain";
diag_log "  11= Airport site";
diag_log "";

/// 1: TYPELESS METHOD
///
/// Uses sectors named sX where X is an increment.
/// It does not convey any sector type.
///
_spot_check = true;
_spot_id = 0;
_missing = 0;
while { _spot_check } do {
	_type = 0;
	_spot_id = _spot_id + 1;
	_spot_name = Format["s%1", _spot_id];
	_spot_pos = getMarkerPos _spot_name;
	if ((_spot_pos distance [0,0,0]) > 10) then {
		_spot_unique_name = MarkerText _spot_name;
		if (_spot_unique_name == "") then { _spot_unique_name = _spot_name; };
		_sector_id = -1;
		if (count _sector_list > 0) then {
			for [{_i=0}, {_i<(count _sector_list)}, {_i=_i+1}] do
			{
				_sector = _sector_list select _i;
				_valid = [_spot_pos, _sector select XF_SECTOR_ARRAY_SHAPE_ARRAY] call XfPointInShapeArray;
				// diag_log Format["Tested Sector %1 : %2",_sector, _valid];
				if (_valid) exitWith { _sector_id = _i; };
			};
		};
		diag_log Format["spot:[%1,'%2','%2',%3,%4,%5,1,True,[0,0]],",_spot_pos,_spot_unique_name, (getMarkerSize _spot_name) select 0, _type, _sector_id];
		_new_location = [];
		_new_location set [XF_MAP_LOCATION_CENTER, _spot_pos];
		_new_location set [XF_MAP_LOCATION_DISPLAYNAME, _spot_unique_name];
		_new_location set [XF_MAP_LOCATION_UNIQUENAME, _spot_unique_name];
		_new_location set [XF_MAP_LOCATION_SIZE, (getMarkerSize _spot_name) select 0];
		_new_location set [XF_MAP_LOCATION_TYPE, _type];
		_new_location set [XF_MAP_LOCATION_MISSIONSECTOR, _sector_id];
		_new_location set [XF_MAP_LOCATION_CIVILDENSITY, 1];
		_new_location set [XF_MAP_LOCATION_ROADBLOCK, true];
		_new_location set [XF_MAP_LOCATION_AEGSECTION, [0,0]];
		D_LOCATIONS set [ count D_LOCATIONS, _new_location ];
		_missing = 0;
	} else {
		_missing = _missing + 1;
		if (_missing > 100) then {
			_spot_check = false;
		};
	};
};

/// 2: TYPE AWARE METHOD
///
/// Uses sectors named sX_Y where X is the type of the sector and Y is an increment.
/// It DOES convey any sector type.
///
for [{_type = 0;},{_type < 12 },{_type = _type + 1}] do {
	_spot_check = true;
	_spot_id = 0;
	_missing = 0;
	_roadblock = (_type in [XF_MAP_LOCATION_TYPE_TOWN,XF_MAP_LOCATION_TYPE_VILLAGE]);
	while { _spot_check } do {
		_spot_id = _spot_id + 1;
		_spot_name = Format["s%1_%2", _type, _spot_id];
		_spot_pos = getMarkerPos _spot_name;
		if ((_spot_pos distance [0,0,0]) > 10) then {
			_spot_unique_name = MarkerText _spot_name;
			if (_spot_unique_name == "") then { _spot_unique_name = _spot_name; };
			_sector_id = -1;
			if (count _sector_list > 0) then {
				for [{_i=0}, {_i<(count _sector_list)}, {_i=_i+1}] do
				{
					_sector = _sector_list select _i;
					_valid = [_spot_pos, _sector select XF_SECTOR_ARRAY_SHAPE_ARRAY] call XfPointInShapeArray;
					// diag_log Format["Tested Sector %1 : %2",_sector, _valid];
					if (_valid) exitWith { _sector_id = _i; };
				};
			};
			diag_log Format["typed:[%1,'%2','%2',%3,%4,%5,1,True,[0,0]],",_spot_pos,_spot_unique_name, (getMarkerSize _spot_name) select 0,_type, _sector_id];
			_new_location = [];
			_new_location set [XF_MAP_LOCATION_CENTER, _spot_pos];
			_new_location set [XF_MAP_LOCATION_DISPLAYNAME, _spot_unique_name];
			_new_location set [XF_MAP_LOCATION_UNIQUENAME, _spot_unique_name];
			_new_location set [XF_MAP_LOCATION_SIZE, (getMarkerSize _spot_name) select 0];
			_new_location set [XF_MAP_LOCATION_TYPE, _type];
			_new_location set [XF_MAP_LOCATION_MISSIONSECTOR, _sector_id];
			_new_location set [XF_MAP_LOCATION_CIVILDENSITY, 1];
			_new_location set [XF_MAP_LOCATION_ROADBLOCK, _roadblock];
			_new_location set [XF_MAP_LOCATION_AEGSECTION, [0,0]];
			D_LOCATIONS set [ count D_LOCATIONS, _new_location ];
			_missing = 0;
		} else {
			_missing = _missing + 1;
			if (_missing > 100) then {
				_spot_check = false;
			};
		};
	};
};

_spot_id = 0;
_missing = 0;
_spot_check = true;
while { _spot_check } do {
	_spot_id = _spot_id + 1;
	_spot_name = Format["int_%1",_spot_id];
	_spot_pos = getMarkerPos _spot_name;
	if ((_spot_pos distance [0,0,0]) > 10) then {
		_spot_name setMarkerText Format[" %1",_spot_id];
		_missing = 0;
	} else {
		_missing = _missing + 1;
		if (_missing > 100) then {
			_spot_check = false;
		};
	};
};


waituntil { !isnull player; };
_center_pos = getArray (configFile>>"CfgWorlds">>worldName >> "centerPosition");
_center_pos set [2, 0];
"map_center" setMarkerPos _center_pos;

D_PLAYER_START_POS = getPos player;
D_PLAYER_START_DIR = getDir player;
D_BUILDING_POS = getMarkerPos "Buildings";
if ((D_BUILDING_POS distance [0,0,0]) < 10) then {
	D_BUILDING_POS = player modelToWorld [0, 50, 0];
};
call compile preprocessFileLineNumbers "buildingposition.sqf";

if (!isnil "BUILDING_POSITION_INDEXER" && !isnil "BUILDING_POSITION_INFOS") then {
	D_BUILDING_INDEXER = [D_BUILDING_INDEXER, BUILDING_POSITION_INDEXER] call XfArrayPushStack;
	D_BUILDING_TABLE = [D_BUILDING_TABLE, BUILDING_POSITION_INFOS] call XfArrayPushStack;
	hint "Loaded existing building table";
};

player addaction ["Teleport","teleport.sqf",[],0,False,True,"","true"];
player addaction ["Building entrance position","Infos\buildingpos.sqf",[1],10,False,True,"","true"];
player addaction ["Building hiding position","Infos\buildingpos.sqf",[2],10,False,True,"","true"];
player addaction ["Building sniper position","Infos\buildingpos.sqf",[3],10,False,True,"","true"];
player addaction ["Teleport Up 5","Infos\position_teleport.sqf",[5],8,False,True,"","true"];
player addaction ["Teleport Up 7","Infos\position_teleport.sqf",[7],7,False,True,"","true"];
player addaction ["Teleport Up 10","Infos\position_teleport.sqf",[10],6,False,True,"","true"];
player addaction ["Teleport Up 15","Infos\position_teleport.sqf",[15],5,False,True,"","true"];
player addaction ["Teleport Up 20","Infos\position_teleport.sqf",[20],4,False,True,"","true"];
player addaction ["Cycle through buildings","Infos\cycle_buildings.sqf",[],2,False,True,"","true"];
player addaction ["Test building positions","Infos\test_building.sqf",[],20,False,True,"","true"];
player addaction ["Create Building Array","Infos\create_building_pos.sqf",[],20,False,True,"","true"];

execVM "R3F_DEBUG\scripts\functions_R3F_Debug.sqf";

